package hamaster.jpbc.gui;

import static javax.swing.BoxLayout.Y_AXIS;
import hamaster.jpbc.Main;
import hamaster.jpbc.crypt.aes.OperationMode;
import hamaster.jpbc.userconfig.ConfigKeys;
import hamaster.jpbc.userconfig.UserConfig;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class AESConfigPanel extends JPanel implements ActionListener, ItemListener {
	private static final long serialVersionUID = 3818527164032780074L;

	private JButton ok;
	private JButton cancel;
	private ConfigPanel container;
	private JComboBox modeCombo;
	private JComboBox lengthCombo;
	private boolean configChanged;

	public AESConfigPanel(ConfigPanel container) {
		setLayout(new BoxLayout(this, Y_AXIS));
		this.container = container;
		configChanged = false;
		JPanel title = new JPanel(new FlowLayout(FlowLayout.LEFT));
		title.add(new JLabel(Messages.getString("AESConfigPanel.0"))); //$NON-NLS-1$

		JPanel keyLengthCfg = new JPanel(new FlowLayout(FlowLayout.LEFT));
		keyLengthCfg.add(new JLabel(Messages.getString("AESConfigPanel.1"))); //$NON-NLS-1$
		Integer[] lengthArray = new Integer[] {128, 192, 256};
		lengthCombo = new JComboBox(lengthArray);
		keyLengthCfg.add(lengthCombo);
		keyLengthCfg.add(new JLabel(Messages.getString("AESConfigPanel.2"))); //$NON-NLS-1$
		lengthCombo.addItemListener(this);

		JPanel operModeCfg = new JPanel(new FlowLayout(FlowLayout.LEFT));
		operModeCfg.add(new JLabel(Messages.getString("AESConfigPanel.3"))); //$NON-NLS-1$
		OperationMode[] modes = new OperationMode[] {OperationMode.CBC, OperationMode.CFB, OperationMode.OFB};
		modeCombo = new JComboBox(modes);
		operModeCfg.add(modeCombo);
		modeCombo.addItemListener(this);

		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		ok = new JButton(Messages.getString("AESConfigPanel.4")); //$NON-NLS-1$
		cancel = new JButton(Messages.getString("AESConfigPanel.5")); //$NON-NLS-1$
		ok.addActionListener(this);
		cancel.addActionListener(this);
		buttonPanel.add(cancel);
		buttonPanel.add(ok);

		add(title);
		add(keyLengthCfg);
		add(operModeCfg);
		add(buttonPanel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.ok) {
			try {
				int length = Integer.parseInt(lengthCombo.getSelectedItem().toString());
				OperationMode mode = Enum.valueOf(OperationMode.class, modeCombo.getSelectedItem().toString());
				UserConfig userConfig = Main.getUserConfig();
				userConfig.setProperty(ConfigKeys.AES_PREFIX + ConfigKeys.SYMM_KEY_LENGTH, length + ""); //$NON-NLS-1$
				userConfig.setProperty(ConfigKeys.AES_PREFIX + ConfigKeys.OPERATION_MODE, mode.toString());
				container.application.setUserConfig(userConfig);
				JOptionPane.showMessageDialog(container, Messages.getString("AESConfigPanel.7"), Messages.getString("AESConfigPanel.8"), JOptionPane.INFORMATION_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
			} catch (NumberFormatException e1) {
				// TODO handle exception
				e1.printStackTrace();
			}
		}
		if (e.getSource() == this.cancel) {
			if (!configChanged) {
				container.setVisible(false);
				return;
			}
			int doCancel = JOptionPane.showConfirmDialog(container, Messages.getString("AESConfigPanel.9"), Messages.getString("AESConfigPanel.10"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
			if (doCancel == JOptionPane.YES_OPTION)
				container.setVisible(false);
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			configChanged = true;
		}
	}
}
